/*
 * FileEditorDialog.java
 * Created on Aug 6, 2011, 4:20:24 PM
 */
package org.bm.graphexplorer.components;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import org.bm.graphexplorer.controllers.GraphController;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.Task;

/**
 *
 * @author elisha
 */
public final class FileEditorDialog extends javax.swing.JDialog implements PropertyChangeListener {

    /** Creates new form FileEditorDialog */
    public FileEditorDialog(java.awt.Frame parent) {
        super(parent, false);
        initComponents();
    }

    public void setController(GraphController gc) {
        File f = gc.getFile();
        if (f != null)
            try {
                textArea.read(new FileReader(f), null);
                textArea.setEditable(true);
            } catch (IOException ex) {
                Logger.getLogger(FileEditorDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
        else {
            textArea.setText("File must be saved to activate editor dialog!");
            textArea.setEditable(false);
        }
    }

    public void propertyChange(PropertyChangeEvent evt) {  
        if (evt.getPropertyName().equals("activeController"))
            setController((GraphController) evt.getNewValue());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        saveB = new javax.swing.JButton();
        applyB = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();

        setTitle("Edit Graph File");

        jToolBar1.setRollover(true);
        jToolBar1.setName("jToolBar1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(org.bm.graphexplorer.GraphExplorerApp.class).getContext().getActionMap(FileEditorDialog.class, this);
        saveB.setAction(actionMap.get("saveChanges")); // NOI18N
        saveB.setEnabled(false);
        saveB.setFocusable(false);
        saveB.setHideActionText(true);
        saveB.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        saveB.setName("saveB"); // NOI18N
        saveB.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(saveB);

        applyB.setAction(actionMap.get("applyChanges")); // NOI18N
        applyB.setEnabled(false);
        applyB.setFocusable(false);
        applyB.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        applyB.setName("applyB"); // NOI18N
        applyB.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(applyB);

        getContentPane().add(jToolBar1, java.awt.BorderLayout.NORTH);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        textArea.setColumns(20);
        textArea.setLineWrap(true);
        textArea.setRows(5);
        textArea.setText("This experimental feature currently only works for graphs that have been saved to a file."); // NOI18N
        textArea.setWrapStyleWord(true);
        textArea.setEnabled(false);
        textArea.setName("textArea"); // NOI18N
        jScrollPane1.setViewportView(textArea);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public Task applyChanges() {
        return new Task<Object, Void>(Application.getInstance()) {
            @Override
            protected Object doInBackground() throws Exception {
                return null;
            }
            @Override 
            protected void succeeded(Object result) {
                JOptionPane.showMessageDialog(FileEditorDialog.this, "Option disabled.");
            }
        };
    }

    @Action
    public Task saveChanges() {
        return new Task<Object, Void>(Application.getInstance()) {
            @Override
            protected Object doInBackground() throws Exception {
                return null;
            }
            @Override 
            protected void succeeded(Object result) {
                JOptionPane.showMessageDialog(FileEditorDialog.this, "Option disabled.");
            }
        };
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton applyB;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton saveB;
    private javax.swing.JTextArea textArea;
    // End of variables declaration//GEN-END:variables
}
